// ChatServerDlg.h : header file
//
#define PORT 25000
#define WM_SOCKET WM_USER+1
using namespace std;
#include <list>
#pragma once
#include "afxwin.h"
#include "afxcmn.h"
#include "AccountManagement.h"
#include "LMErr.h"

struct Client {
	SOCKET socket;
	CString name;
	int msgType;
	int buffLength;
	CString groupType;
};

// CChatServerDlg dialog
class CChatServerDlg : public CDialog
{
public:
	void InitListControl();
	void LoadAccountList();
	CString ToCString(int iNum);
// Construction
public:
	CChatServerDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_CHATSERVER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	LRESULT SockMsg(WPARAM wParam, LPARAM lParam);
	void BroadClients(CString annstr,list<Client>::iterator i);
	void BroadClientsNotWithoutName(CString annstr,list<Client>::iterator i);
	void HandleNameClient(CString msg,list<Client>::iterator i);
	void SendMember(CString name, SOCKET sclient,char type);
// Implementation
protected:
	HICON m_hIcon;

	SOCKET sockServer,sockClient;
	struct sockaddr_in serverAdd;
	CString	m_msgString;
	Client client;
	list<Client> list_client;
	bool isClose;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedButtonopenconnection();
	afx_msg void OnBnClickedButtoncloseconnection();
	afx_msg void OnBnClickedButtonquit();
	afx_msg void OnClose();
	//CListBox m_listname;
	CEdit m_msg;
	CString m_Strmsg;
	CListCtrl m_lstUser;
	AccountManager* accMgr;
	CListBox m_listname;
	CComboBox m_cmbGroup;
	Account* accClick;
	afx_msg void OnBnClickedButtonupdategroup();
	afx_msg void OnNMClickListuser(NMHDR *pNMHDR, LRESULT *pResult);
	void ManageAccount(CString msg,list<Client>::iterator i);
	int CreateAccount(CString msg);
	void SendMessageToClient(CString msg,SOCKET sClient);
	bool LogIn(CString msg);
	void HandleOnlineNameClient(CString msg,list<Client>::iterator i);
	void HandleClientLogOut(list<Client>::iterator i);
	void UpdateUserStatus(CString name,bool flag);//true = ONLINE, fasle = OFF
	void HandleChat(CString msg,list<Client>::iterator i);
};
